import type { REPORT_ID } from '@/enum'
import { REPORT_AUDIT_STATUS, REPORT_AUDIT_STATUS_TEXT } from '@/enum'
import { queryReportAuditList, exportFileOfReport, auditReport, loadEnterpriseListOfReport, auditExportReportStatusAll } from '@/api/modules/dataReport'
import { PAGING } from '@/constant'

export interface ListItem {
  id?: number
  enterprise: string
  socialCreditCode: string
  name: string
  tId: REPORT_ID
  status: REPORT_AUDIT_STATUS
  remark: string
  affiliatedUnit: string
}

export interface QueryListParams {
  month: string
  status?: REPORT_AUDIT_STATUS
  socialCreditCode?: string
  reportName?: string
}

export interface EnterpriseItem {
  socialCreditCode: string
  subsidiaryName: string
}

export function useLoadData() {
  const tableData = ref<ListItem[]>([])
  const loading = ref(false)
  const pagination = reactive({
    total: 0,
    pageSize: PAGING.PAGE_SIZES[0],
    pageNumber: 1,
  })
  const enterpriseList = shallowRef<EnterpriseItem[]>([])

  const designStore = useDesignStore()

  const statusOptions = shallowRef([
    { label: REPORT_AUDIT_STATUS_TEXT.UNDER_REPORT, value: REPORT_AUDIT_STATUS.UNDER_REPORT },
    { label: REPORT_AUDIT_STATUS_TEXT.STAGED, value: REPORT_AUDIT_STATUS.STAGED },
    { label: REPORT_AUDIT_STATUS_TEXT.REPORTED, value: REPORT_AUDIT_STATUS.REPORTED },
    { label: REPORT_AUDIT_STATUS_TEXT.RESOLVED, value: REPORT_AUDIT_STATUS.RESOLVED },
    { label: REPORT_AUDIT_STATUS_TEXT.REJECTED, value: REPORT_AUDIT_STATUS.REJECTED },
  ])

  async function queryList(params: QueryListParams) {
    loading.value = true
    let data = [] as ListItem[]
    try {
      const { data: res } = await queryReportAuditList({
        time: params.month,
        status: params.status,
        socialCreditCode: params.socialCreditCode,
        reportName: params.reportName,
        pageNumber: pagination.pageNumber,
        pageSize: pagination.pageSize,
      })
      data = (res.records ?? []).map((v: any) => ({
        id: v.id,
        enterprise: v.subsidiaryName,
        // TODO
        socialCreditCode: v.socialCreditCode,
        name: v.reportName,
        tId: v.reportId,
        status: v.status,
        remark: v.remark,
        affiliatedUnit: v.affiliatedUnit,
      }))
      tableData.value = data
      pagination.total = res.total
    }
    finally {
      loading.value = false
    }
    return data
  }

  function getStatusText(status: REPORT_AUDIT_STATUS) {
    let result = '' as REPORT_AUDIT_STATUS_TEXT
    switch (status) {
      case REPORT_AUDIT_STATUS.UNDER_REPORT:
        result = REPORT_AUDIT_STATUS_TEXT.UNDER_REPORT
        break
      case REPORT_AUDIT_STATUS.STAGED:
        result = REPORT_AUDIT_STATUS_TEXT.STAGED
        break
      case REPORT_AUDIT_STATUS.REPORTED:
        result = REPORT_AUDIT_STATUS_TEXT.REPORTED
        break
      case REPORT_AUDIT_STATUS.RESOLVED:
        result = REPORT_AUDIT_STATUS_TEXT.RESOLVED
        break
      case REPORT_AUDIT_STATUS.REJECTED:
        result = REPORT_AUDIT_STATUS_TEXT.REJECTED
        break
    }
    return result
  }

  // 按钮 actions 逻辑 查看-1，历史操作-2，导出-3，审核-4
  // 未填写 -> 无
  // 已填写 -> 查看、历史操作、导出
  // 已上报 -> 查看、审核、历史操作、导出
  // 已审核 -> 查看、审核、历史操作、导出
  // 退回   -> 查看、历史操作、导出

  function showAction(status: REPORT_AUDIT_STATUS, t: number): boolean {
    switch (status) {
      case REPORT_AUDIT_STATUS.UNDER_REPORT:
        return false
      case REPORT_AUDIT_STATUS.STAGED:
      case REPORT_AUDIT_STATUS.REJECTED:
        return ([1, 2, 3]).includes(t)
      case REPORT_AUDIT_STATUS.REPORTED:
      case REPORT_AUDIT_STATUS.RESOLVED:
        return ([1, 2, 3, 4]).includes(t)
      default:
        return false
    }
  }

  async function exportFile(id: number) {
    await exportFileOfReport(id)
  }

  async function exportReportStatusAll(params: QueryListParams) {
    await auditExportReportStatusAll({
      time: params.month,
      status: params.status,
      socialCreditCode: params.socialCreditCode,
      reportName: params.reportName,
    })
  }

  async function audit(id: number, type: 0 | 1) {
    if (type === 1) {
      // 退回
      const { value: remark } = await ElMessageBox.prompt('请输入退回原因', '退回', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPlaceholder: '请输入退回原因',
        inputErrorMessage: '请输入退回原因',
        inputValidator: (value) => {
          if (!value) {
            return '请输入退回原因'
          }
          return true
        },
      })
      await auditReport({
        id,
        type,
        remark,
      })
    }
    else {
      await auditReport({
        id,
        type,
      })
    }
    ElMessage.success('操作成功')
  }

  async function loadEnterpriseList() {
    const { data } = await loadEnterpriseListOfReport()
    enterpriseList.value = data
  }

  function getTagColor(status: REPORT_AUDIT_STATUS): string {
    switch (status) {
      case REPORT_AUDIT_STATUS.UNDER_REPORT:
        return 'yellow'
      case REPORT_AUDIT_STATUS.STAGED:
        return designStore.isDark ? 'white' : 'black'
      case REPORT_AUDIT_STATUS.REPORTED:
        return 'green'
      case REPORT_AUDIT_STATUS.RESOLVED:
        return 'blue'
      case REPORT_AUDIT_STATUS.REJECTED:
        return 'red'
    }
  }

  loadEnterpriseList()

  return {
    loading,
    tableData,
    statusOptions,
    pagination,
    enterpriseList,
    queryList,
    getStatusText,
    showAction,
    exportFile,
    audit,
    getTagColor,
    exportReportStatusAll,
  }
}
